﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace Titan.Test
{
    [DataContract]
    public class DynamicEntity : IDynamicEntity
    {
        //需要区分大小写 
        private Dictionary<string, object> properties = new Dictionary<string, object>();

        public DynamicEntity()
        {
        }

        public DynamicEntity(string typeName)
        {
            this.TypeName = typeName;
        }



        public void Set<T>(string propertyName, T value)
        {
            this[propertyName] = value;
        }

        public T Get<T>(string propertyName)
        {
            return properties.ContainsKey(propertyName) ? (T)properties[propertyName] : default(T);
        }

        [DataMember]
        public virtual string TypeName { get; set; }

        public virtual object Get(string propertyName)
        {
            return properties.ContainsKey(propertyName) ? properties[propertyName] : null;
        }
        public virtual void Set(string propertyName, object value)
        {
            if (properties.ContainsKey(propertyName))
            {
                properties[propertyName] = value;
            }
            else
            {
                properties.Add(propertyName, value);
            }
        }
        public virtual object this[string propertyName]
        {
            get
            {
                return properties.ContainsKey(propertyName) ? properties[propertyName] : null;
            }
            set
            {
                if (properties.ContainsKey(propertyName))
                {
                    properties[propertyName] = value;
                }
                else
                {
                    properties.Add(propertyName, value);
                }
            }
        }

        public bool Contains(string propertyName)
        {
            return properties.ContainsKey(propertyName);
        }

        //public ICollection<string> PropertyNames
        //{
        //    get { return properties.Keys; }
        //}

        [DataMember]
        public IDictionary<string, object> Properties
        {
            get { return properties; }
        }

        public bool Remove(string propertyName)
        {
            return properties.Remove(propertyName);
        }

        public bool TryGetValue(string propertyName, out object value)
        {
            return properties.TryGetValue(propertyName, out value);
        }

        //public ICollection<object> Values
        //{
        //    get { return properties.Values; }
        //}

        public void Clear()
        {
            properties.Clear();
        }

        public int Count
        {
            get { return properties.Count; }
        }


    }
}
